# -*- coding: utf-8 -*-
# @Author：fanmingxiang@touty.io
# @Date  ：2022/2/15 16:43
from selenium.webdriver.common.by import By
from appium import webdriver
from time import sleep
from selenium.webdriver.support.wait import WebDriverWait


desired_caps = {
    "platformName": "Android",
    "platformVersion": "10",
    "deviceName": "xxx",
    "appPackage": "com.tencent.mm",
    "appActivity": ".ui.LauncherUI",
    "noReset": True,
    "automationName": "UiAutomator2",
    "chromeOptions": {'androidProcess': 'com.tencent.mm:toolsmp'}
}

# 连接手机
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

driver.implicitly_wait(5)
# 点击搜索框
driver.find_element(By.ID, 'com.tencent.mm:id/he6').click()

# 搜索框内输入数据
driver.find_element(By.ID, 'com.tencent.mm:id/bxz').send_keys('智能零售平台')
# 点击公众号图片进入
driver.find_element(By.ID, 'com.tencent.mm:id/ir3').click()
# # 一级菜单
# driver.find_element(By.XPATH, '//*[@text="热门活动"]').click()
# 二级菜单
driver.find_element(By.XPATH, '//*[@text="测试九宫格"]').click()
sleep(2)
# 进入h5
driver.switch_to.context('WEBVIEW_com.tencent.mm:toolsmp')
# # 切换至h5
# driver.switch_to.window(driver.window_handles[0])
# number = int(driver.find_element(By.CSS_SELECTOR, '.limit-num').text)
# switch = 0
# for i in range(number):
#     if switch == 0:
#         # #点击开始抽奖
#         driver.find_element(By.CSS_SELECTOR, '.start-btn').click()
#         # 获取中了啥
#         wait = WebDriverWait(driver, timeout=10).until(lambda x: x.find_element(By.CSS_SELECTOR, '.ant-modal-body'))
#         text = wait.text
#     # switch为0 再次抽奖
#     if '就差一点点' in text:
#         driver.find_element(By.CSS_SELECTOR, '.ant-modal-close-x').click()
#         switch = 0
#     else:
#         break
#
# # 返回
# driver.press_keycode(4)
# sleep(form_case)
# # 回到微信
# driver.switch_to.context('NATIVE_APP')
# if '京东悦鲜活12元满减券' in text:
#     driver.find_element(By.XPATH, '(//*[@text="京东悦鲜活12元满减券表单"])[last()]').click()
# if '悦鲜活A2周卡表单' in text:
#     driver.find_element(By.XPATH, '(//*[@text="悦鲜活A2周卡表单"])[last()]').click()
# if '悦鲜活A2月卡表单' in text:
#     driver.find_element(By.XPATH, '(//*[@text="悦鲜活A2月卡表单"])[last()]').click()
driver.find_element(By.XPATH, '(//*[@text="京东悦鲜活12元满减券表单"])[last()]').click()
# 进入h5
driver.switch_to.context('WEBVIEW_com.tencent.mm:toolsmp')
# 切换至h5
driver.switch_to.window(driver.window_handles[0])
# 姓名
driver.find_element(By.XPATH, '//*[@placeholder="输入姓名"]').send_keys('测试')
# 手机号
driver.find_element(By.XPATH, '//*[@placeholder="输入手机号"]').send_keys('17630913145')
# 省市下拉框
# driver.find_element(By.XPATH, '.ant-cascader-picker-label').click()
# # 选省市
# driver.find_element(By.CSS_SELECTOR, '//*[@text="北京市"]').click()
# driver.find_element(By.CSS_SELECTOR, '//*[@text="市辖区"]').click()
# driver.find_element(By.CSS_SELECTOR, '//*[@text="东城区"]').click()
# 输入地址
# driver.find_element(By.XPATH, '//*[@placeholder="输入详细地址"]').send_keys('测试地址123')
# 提交按钮
driver.find_element(By.CSS_SELECTOR, '.ant-btn').click()
